﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CoCaNgua
{
    public partial class Dice : Control
    {
        private Game _g;

        internal Game G
        {
            get { return _g; }
            set { _g = value; }
        }
        private Image _FullImage;
        private int _Value;

        public int Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        public Image FullImage
        {
            get { return _FullImage; }
            set { _FullImage = value; }
        }

        static private System.Drawing.Pen s_ShadowPen = new System.Drawing.Pen(Color.DarkGray);

        // Xay dung contrustor ko doi so de su dung ta` da.o
        // Lan dau code kieu nay :(
        public Dice()
        {
            InitializeComponent();
            this.Value = 1;            
            this.Width = 48;
            this.Height = 48;
            this.Top = 96;
            this.Left = 96;
        }

        public Dice(Image img)
        {
            
            InitializeComponent();
            this.Value = 1;
            FullImage = img;
            this.Width = 48;
            this.Height = 48;
            this.Top = 96;
            this.Left = 96;
            //G = g;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: Add custom paint code here

            // Calling the base class OnPaint
            //pe.Graphics.DrawEllipse(s_ShadowPen, new Rectangle(0, 0, 100, 100));
            pe.Graphics.DrawImage(FullImage, new Rectangle(0,0,48,48), new Rectangle((this.Value - 1) * 100, 0, 100, 100), GraphicsUnit.Pixel);
            base.OnPaint(pe);
        }

        public int Throw()
        {
            Random r = new Random();
            int x = r.Next(2, 10);
            //int y = 0;
            for (int i = 0; i < x; i++)
            {
                Thread.Sleep(500);

                this.Value = r.Next(1, 7);

                // ramdom: 1->6
                this.Value = r.Next(1, 7);

                Parent.Update();
                this.Invalidate();
            }
            //this.Value = y;
            return this.Value;
        }

        private void Dice_Click(object sender, EventArgs e)
        {
            Throw();
        }
    }
}
